package org.jing.r18.game.venusblood;

import org.jing.core.lang.JingException;
import org.jing.core.logger.JingLogger;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterOutputStream;

/**
 * Description: <br>
 *
 * @author: bksmate <br>
 * @createDate: 2025-05-13 <br>
 */
public class Test {
    private static final JingLogger LOGGER = JingLogger.getLogger(Test.class);
    private Test() {
        String content = "hello world";
        testApi(content);
        testStream(content);
    }

    private void testApi(String content) {
        LOGGER.info(">>> test api::begin <<<");
        final boolean nowrap = false;
        byte[] source = content.getBytes(StandardCharsets.UTF_16LE);
        LOGGER.info("0: {}", buffer2String(source));
        try (
            ByteArrayOutputStream inflateBaos = new ByteArrayOutputStream()
        ) {
            byte[] buffer = new byte[1024];
            int[] levels = { -1, 0, 1, 2, 3, 8, 9};
            byte[] deflatedBuffer = null;
            for (int l$ : levels) {
                try (ByteArrayOutputStream deflateBaos = new ByteArrayOutputStream()) {
                    Deflater deflater = new Deflater(l$, nowrap);
                    deflater.setInput(source);
                    deflater.finish();
                    while (!deflater.finished()) {
                        int count = deflater.deflate(buffer);
                        deflateBaos.write(buffer, 0, count);
                    }
                    deflater.end();
                    deflatedBuffer = deflateBaos.toByteArray();
                    LOGGER.info("1@{}: {}", l$, buffer2String(deflatedBuffer));
                }
            }

            Inflater inflater = new Inflater(nowrap);
            inflater.setInput(deflatedBuffer);
            while (!inflater.finished()) {
                int count = inflater.inflate(buffer);
                inflateBaos.write(buffer, 0, count);
            }
            inflater.end();
            byte[] inflatedBuffer = inflateBaos.toByteArray();
            LOGGER.info("2: {}", buffer2String(inflatedBuffer));
        } catch (Exception e) {
            throw new JingException(e);
        }
        LOGGER.info(">>> test api::end <<<");
    }

    private void testStream(String content) {
        LOGGER.info(">>> test stream::begin <<<");
        byte[] source = content.getBytes(StandardCharsets.UTF_16LE);
        LOGGER.info("0: {}", buffer2String(source));
        try (
            ByteArrayOutputStream deflateBaos = new ByteArrayOutputStream();
            ByteArrayOutputStream inflateBaos = new ByteArrayOutputStream();
            DeflaterOutputStream dos = new DeflaterOutputStream(deflateBaos);
            InflaterOutputStream ios = new InflaterOutputStream(inflateBaos)
        ) {
            dos.write(source);
            dos.finish();
            byte[] deflatedBuffer = deflateBaos.toByteArray();
            LOGGER.info("1: {}", buffer2String(deflatedBuffer));

            ios.write(deflatedBuffer);
            ios.finish();
            byte[] inflatedBuffer = inflateBaos.toByteArray();
            LOGGER.info("2: {}", buffer2String(inflatedBuffer));
        } catch (Exception e) {
            throw new JingException(e);
        }
        LOGGER.info(">>> test stream::end <<<");
    }

    private String buffer2String(byte[] buffer) {
        return Arrays.toString(buffer);
    }

    public static void main(String[] args) {
        new Test();
    }
}
